<template>
    <div>
        <!-- 搜索栏 -->
        <el-card class="!border-none mb-4" shadow="never">
            <el-form class="mb-[-16px]" :model="queryParams" :inline="true">
                <el-form-item label="分类名称">
                    <div class="w-[250px]">
                        <el-input
                            v-model="queryParams.name"
                            placeholder="请输入分类名称"
                            clearable
                            @keyup.enter="resetPaging"
                        />
                    </div>
                </el-form-item>
                <el-form-item label="分类状态">
                    <el-select class="!w-[200px]" v-model="queryParams.is_disable">
                        <el-option label="全部" value="" />
                        <el-option label="启用" value="0" />
                        <el-option label="禁用" value="1" />
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPaging">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>

        <!-- 表格栏 -->
        <el-card v-loading="pager.loading" class="!border-none table-card" shadow="never">
            <el-tabs v-model="tabActive" @tabChange="changeTabs">
                <el-tab-pane
                    v-for="item in tabsMap"
                    :label="item.name"
                    :name="item.type"
                    :index="item.type"
                    :key="item.type"
                />
            </el-tabs>

            <el-button type="primary" v-perms="['auth:admin:add']" @click="handleEditor('add')">
                <template #icon>
                    <icon name="el-icon-Plus" />
                </template>
                <span>新增</span>
            </el-button>
            <el-table :data="pager.lists" size="large" class="mt-4">
                <el-table-column label="ID" prop="id" min-width="60" />
                <el-table-column label="图标" min-width="100">
                    <template #default="{ row }">
                        <div class="flex items-center">
                            <el-image v-if="row.icon" :src="row.icon" class="w-[22px] h-[22px]" />
                            <span v-else>-</span>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="分类名称" prop="name" min-width="120" />
                <el-table-column label="已被关联" prop="nums" min-width="120" />
                <el-table-column label="排序" prop="sort" min-width="100" />
                <el-table-column label="状态" prop="is_disable" min-width="80">
                    <template #default="{ row }">
                        <el-tag v-if="row.is_disable === 0">正常</el-tag>
                        <el-tag v-else type="danger">停用</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="120" fixed="right">
                    <template #default="{ row }">
                        <el-button
                            v-perms="['square:category:edit']"
                            type="primary"
                            link
                            @click="handleEditor('edit', row)"
                        >
                            编辑
                        </el-button>
                        <el-button
                            v-perms="['square:category:delete']"
                            type="danger"
                            link
                            @click="handleDelete(row.id)"
                        >
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <paging v-model="pager" @change="queryLists" />
            </div>
        </el-card>

        <!-- 编辑器 -->
        <editor v-if="showEdit" ref="editorRef" @success="queryLists" @close="showEdit = false" />
    </div>
</template>

<script setup lang="ts">
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import squareCategoryApi from '@/api/square/category'
import Editor from './editor.vue'

const showEdit = ref(false)
const editorRef = shallowRef<InstanceType<typeof Editor>>()

const tabActive = ref<string>('robot')
const tabsMap = [
    {
        name: 'AI智脑',
        type: 'robot'
    },
    {
        name: 'AI绘画',
        type: 'draws'
    },
    {
        name: 'AI视频',
        type: 'video'
    },
    {
        name: 'AI音乐',
        type: 'music'
    }
]

// 查询参数
const queryParams = reactive({
    name: '',
    type: tabActive.value,
    is_disable: ''
})

// 分页查询
const { pager, queryLists, resetParams, resetPaging } = usePaging({
    fetchFun: squareCategoryApi.lists,
    params: queryParams
})

/**
 * 切换选项
 *
 * @param {string} type
 * @author zero
 */
const changeTabs = async (type: string) => {
    queryParams.type = type
    await queryLists()
}

/**
 * 处理编辑
 *
 * @param {string} type
 * @param {any} row
 * @returns {Promise<void>}
 */
const handleEditor = async (type: string, row?: any): Promise<void> => {
    showEdit.value = true
    await nextTick()
    editorRef.value?.open(type, row, tabActive.value)
}

/**
 * 处理删除
 *
 * @param {number} id
 * @returns {Promise<void>}
 */
const handleDelete = async (id: number): Promise<void> => {
    feedback.confirm('确定要删除此项数据吗?')
        .then(async () => {
            await squareCategoryApi.delete(id)
            feedback.msgSuccess('删除成功')
            await queryLists()
        }).catch(() => {})
}

onMounted(async () => {
    await queryLists()
})
</script>

<style scoped lang="scss">
.table-card {
    :deep(.el-card__body) {
        padding-top: 5px;
    }
}
</style>
